import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
// 设置全局的请求域名

Vue.prototype.serverUrl = "https://www.moxiongdi.com";

//封装uni.request方法
Vue.prototype.uni_request = function(params) {
	// 设置全局分享参数
	params.showLoading = params.showLoading == undefined ? true : params.showLoading;
	params.checkLogin = params.checkLogin == undefined ? true : params.checkLogin;
	params.method = params.method == undefined ? "POST" : params.method;
	params.header = params.header == undefined ? {
		'X-Requested-With': 'XMLHttpRequest',
		'content-type': 'application/json',
	} : params.header;
	if (params.checkLogin) {
		//请求头里一定要带上用户登录信息,没得商量
		params.header.Authorization = uni.getStorageSync("token");
	}
	/**/
	if (params.showLoading) {
		uni.showLoading({
			mask: true,
			title: (params.showLoadingTitle ? params.showLoadingTitle : "加载中...")
		});
	}
	var loadingState = false;
	uni.request({
		url: this.serverUrl + params.url,
		data: params.data,
		header: params.header,
		method: params.method,
		success: (res) => {
			//更新authorization
			let authorization = res.header.authorization;
			if (authorization) {
				uni.setStorageSync('token', authorization)
			}

			var status = res.data.status;
			var data = res.data.data;
			var msg = res.data.msg;

			if (params.checkLogin) {
				//登录状态:登录异常
				if (status == -1234) {
					uni.hideLoading();
					uni.showModal({
						title: '请登录',
						content: '',
						success: function() {
							uni.navigateTo({
								url: "../login/login"
							});
						}
					});
					return;
				}
			}
			//服务端返回错误
			if (status != 1) {
				if (params.fail) {
					params.fail(res.data);
				} else {
					uni.showToast({
						title: msg,
						duration: 3000,
						"icon": "none"
					});
					return false;
				}
			} else {
				loadingState = true;
				if (params.success) {
					params.success(data);
				}
			}
		},
		error: (res) => {
			console.log(res)
		},
		complete: () => {
			if (params.showLoading && loadingState) {
				uni.hideLoading();
			}
		}
	});
}





App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif